/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.UniqueConstraint;

/**
 *
 * @author Wei Haoyang
 */
@Entity
@DiscriminatorValue("MonthlyDetailEntity")
@Table(
        name="MonthlyDetailEntity", 
        uniqueConstraints=
            @UniqueConstraint(columnNames={"date"})
    )
public class MonthlyDetailEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date = new Date();
//    private int demandA;
//    private int demandB;
//    private int demandC;
//    private int demandD;
    private int scheduleA;
    private int scheduleB;
    private int scheduleC;
    private int scheduleD;
    private int reservedA;
    private int reservedB;
    private int reservedC;
    private int reservedD;
    private Timestamp ts = new Timestamp(date.getTime());
    @OneToMany(targetEntity = DailyDetailEntity.class, cascade = {CascadeType.ALL}, mappedBy="month")
    private List<DailyDetailEntity> days = new ArrayList<DailyDetailEntity>();
    private float totalTime = 0;

    public Long getId() {
        return id;
    }

    public void calculateTotalTime() {
        float a, b;
        a = scheduleA + scheduleB;
        b = scheduleC + scheduleD;
        a = a / 50;
        b = b / 45;
        totalTime = a + b;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof MonthlyDetailEntity)) {
            return false;
        }
        MonthlyDetailEntity other = (MonthlyDetailEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "mrp.entity.MonthlyDetailEntity[ id=" + id + " ]";
    }

    public Date getDate() {
        return date;
       
    }

    public void setDate(Date date) {
        this.date = date;
    }

//    public int getDemandA() {
//        return demandA;
//    }
//
//    public void setDemandA(int demandA) {
//        this.demandA = demandA;
//    }
//
//    public int getDemandB() {
//        return demandB;
//    }
//
//    public void setDemandB(int demandB) {
//        this.demandB = demandB;
//    }
//
//    public int getDemandC() {
//        return demandC;
//    }
//
//    public void setDemandC(int demandC) {
//        this.demandC = demandC;
//    }
//
//    public int getDemandD() {
//        return demandD;
//    }
//
//    public void setDemandD(int demandD) {
//        this.demandD = demandD;
//    }

    public int getScheduleA() {
        return scheduleA;
    }

    public void setScheduleA(int scheduleA) {
        this.scheduleA = scheduleA;
    }

    public int getScheduleB() {
        return scheduleB;
    }

    public void setScheduleB(int scheduleB) {
        this.scheduleB = scheduleB;
    }

    public int getScheduleC() {
        return scheduleC;
    }

    public void setScheduleC(int scheduleC) {
        this.scheduleC = scheduleC;
    }

    public int getScheduleD() {
        return scheduleD;
    }

    public void setScheduleD(int scheduleD) {
        this.scheduleD = scheduleD;
    }

    public List<DailyDetailEntity> getDays() {
        return days;
    }

    public void setDays(List<DailyDetailEntity> days) {
        this.days = days;
    }

    public int getReservedA() {
        return reservedA;
    }

    public void setReservedA(int reservedA) {
        this.reservedA = reservedA;
    }

    public int getReservedB() {
        return reservedB;
    }

    public void setReservedB(int reservedB) {
        this.reservedB = reservedB;
    }

    public int getReservedC() {
        return reservedC;
    }

    public void setReservedC(int reservedC) {
        this.reservedC = reservedC;
    }

    public int getReservedD() {
        return reservedD;
    }

    public void setReservedD(int reservedD) {
        this.reservedD = reservedD;
    }
    
    public void calculateProduction(){
        this.setScheduleA(0);
        this.setScheduleB(0);
        this.setScheduleC(0);
        this.setScheduleD(0);
        for (DailyDetailEntity day: this.days){
            this.scheduleA += day.getScheduleA();
            this.reservedA += day.getReservedA();
            this.scheduleB += day.getScheduleB();
            this.reservedB += day.getReservedB();
            this.scheduleC += day.getScheduleC();
            this.reservedC += day.getReservedC();
            this.scheduleD += day.getScheduleD();
            this.reservedD += day.getReservedD();
        }
    }

    public Timestamp getTs() {
        return ts;
    }

    public void setTs(Timestamp ts) {
        this.ts = ts;
    }

    /**
     * @return the totalTime

    /**
     * @param totalTime the totalTime to set
     */
    public void setTotalTime(int totalTime) {
        this.setTotalTime(totalTime);
    }

    /**
     * @return the totalTime
     */
    public float getTotalTime() {
        return totalTime;
    }

    /**
     * @param totalTime the totalTime to set
     */
    public void setTotalTime(float totalTime) {
        this.totalTime = totalTime;
    }

    public String getMonthForReport() {
        String dateForReport;
        DateFormat formatter;
        formatter = new SimpleDateFormat("MMM-yyyy");
        dateForReport = formatter.format(this.date);

        return dateForReport;
    }
    
}
